{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "        <script type=\"text/javascript\">\n",
       "        window.PlotlyConfig = {MathJaxConfig: 'local'};\n",
       "        if (window.MathJax) {MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}\n",
       "        if (typeof require !== 'undefined') {\n",
       "        require.undef(\"plotly\");\n",
       "        requirejs.config({\n",
       "            paths: {\n",
       "                'plotly': ['https://cdn.plot.ly/plotly-latest.min']\n",
       "            }\n",
       "        });\n",
       "        require(['plotly'], function(Plotly) {\n",
       "            window._Plotly = Plotly;\n",
       "        });\n",
       "        }\n",
       "        </script>\n",
       "        "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "ename": "UnsupportedOperation",
     "evalue": "not writable",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUnsupportedOperation\u001b[0m                      Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-1832dc70af5e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     59\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     60\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'__main__'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 61\u001b[1;33m     \u001b[0mapp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdebug\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mport\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m8000\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\flask\\app.py\u001b[0m in \u001b[0;36mrun\u001b[1;34m(self, host, port, debug, load_dotenv, **options)\u001b[0m\n\u001b[0;32m    936\u001b[0m         \u001b[0moptions\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msetdefault\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'threaded'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    937\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 938\u001b[1;33m         \u001b[0mcli\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshow_server_banner\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0menv\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdebug\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    939\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    940\u001b[0m         \u001b[1;32mfrom\u001b[0m \u001b[0mwerkzeug\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mserving\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mrun_simple\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\flask\\cli.py\u001b[0m in \u001b[0;36mshow_server_banner\u001b[1;34m(env, debug, app_import_path, eager_loading)\u001b[0m\n\u001b[0;32m    627\u001b[0m             \u001b[0mmessage\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;34m' (lazy loading)'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    628\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 629\u001b[1;33m         \u001b[0mclick\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mecho\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    630\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    631\u001b[0m     \u001b[0mclick\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mecho\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m' * Environment: {0}'\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0menv\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\click\\utils.py\u001b[0m in \u001b[0;36mecho\u001b[1;34m(message, file, nl, err, color)\u001b[0m\n\u001b[0;32m    257\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    258\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mmessage\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 259\u001b[1;33m         \u001b[0mfile\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    260\u001b[0m     \u001b[0mfile\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mflush\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    261\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mUnsupportedOperation\u001b[0m: not writable"
     ]
    }
   ],
   "source": [
    "from flask import Flask, render_template, request\n",
    "import pandas as pd\n",
    "import cufflinks as cf\n",
    "import plotly as py\n",
    "import plotly.graph_objs as go\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Line\n",
    "\n",
    "df1 = pd.read_csv(\"School_enrollment.csv\",encoding = 'gbk')\n",
    "\n",
    "\n",
    "app = Flask(__name__)\n",
    "\n",
    "# 准备工作\n",
    "\n",
    "regions_available = list(df1.country.dropna().unique())\n",
    "# cf.set_config_file(offline=True, theme=\"ggplot\")\n",
    "# py.offline.init_notebook_mode()\n",
    "\n",
    "\n",
    "\n",
    "@app.route('/',methods=['GET'])\n",
    "def hu_run_2019():\n",
    "    data_str = df1.to_html()\n",
    "    return render_template('results2.html',\n",
    "                           the_res = data_str,\n",
    "                           the_select_region=regions_available)\n",
    "\n",
    "@app.route('/hurun',methods=['POST'])\n",
    "def hu_run_select() -> 'html':\n",
    "    the_region = request.form[\"the_region_selected\"]\n",
    "    print(the_region) # 检查用户输入\n",
    "    dfs = df1.query(\"country=='{}'\".format(the_region))\n",
    "#     df_summary = dfs.groupby(\"行业\").agg({\"企业名称\":\"count\",\"估值（亿人民币）\":\"sum\",\"成立年份\":\"mean\"}).sort_values(by = \"企业名称\",ascending = False )\n",
    "#     print(df_summary.head(5)) # 在后台检查描述性统计\n",
    "#     ## user select\n",
    "    # print(dfs)\n",
    "#     # 交互式可视化画图\n",
    "    fig = dfs.iplot(kind=\"bar\", x=\"country\", asFigure=True)\n",
    "    py.offline.plot(fig, filename=\"example1.html\",auto_open=False)\n",
    "    with open(\"example1.html\", encoding=\"utf8\", mode=\"r\") as f:\n",
    "        plot_all = \"\".join(f.readlines())\n",
    "\n",
    "#     # plotly.offline.plot(data, filename='file.html')\n",
    "\t\n",
    "#     with open(\"render.html\", encoding=\"utf8\", mode=\"r\") as f:\n",
    "#         plot_all = \"\".join(f.readlines())\n",
    "\t\t\t\n",
    "    data_str = dfs.to_html()\n",
    "    return render_template('results2.html',\n",
    "                            the_plot_all = plot_all,\n",
    "\t\t\t\t\t\t\t# the_plot_all = [],\n",
    "                            the_res = data_str,\n",
    "                            the_select_region=regions_available,\n",
    "                           )\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    app.run(debug=True,port=8000)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
